package cn.javaeye.lonlysky.lforum.web.admin.web;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import org.xidea.lite.servlet.Dict;
import org.xidea.lite.servlet.FCKeditorWrapper;

import cn.javaeye.lonlysky.lforum.ForumBaseAction;
import cn.javaeye.lonlysky.lforum.comm.LForumRequest;
import cn.javaeye.lonlysky.lforum.entity.forum.Users;
import cn.javaeye.lonlysky.lforum.entity.web.Article;
import cn.javaeye.lonlysky.lforum.entity.web.FartherType;
import cn.javaeye.lonlysky.lforum.entity.web.ParentType;
import cn.javaeye.lonlysky.lforum.service.ArticleManager;
import cn.javaeye.lonlysky.lforum.service.FartherTypeManager;
import cn.javaeye.lonlysky.lforum.service.ParentTypeManager;

public class ArticleAction extends ForumBaseAction{
	public Users getUsers() {
		return users;
	}

	public void setUsers(Users users) {
		this.users = users;
	}

	private static final long serialVersionUID = -1455415807493601492L;
	private ArticleManager articleManager;
	private Article article;
	private Users users;
	private List<FartherType> fartherTypeList;
	private FartherTypeManager fartherTypeManager;
	private String edit;
	private String dicter;
	private List<Article> articleTypeList;
	protected StringBuilder htmlBuilder = new StringBuilder();

	private ParentTypeManager parentTypeManager;
	private List<ParentType> parentTypeList;
	@Override
	public String execute() throws Exception {
		if (LForumRequest.getParamValue("list").equals("true")) {
				setArticleTypeList(articleManager.findAll());
			return SUCCESS;
		}		
		if (LForumRequest.getParamValue("add").equals("true")) {
			FCKeditorWrapper fck=new FCKeditorWrapper(request);
			edit=fck.get("article.content", "Some text");
			setFartherTypeList(fartherTypeManager.findByType("0"));
			return INPUT;
		}
		if (LForumRequest.getParamValue("remove").equals("true")) {
			articleManager.remove(LForumRequest.getParamIntValue("id",-1));
			setArticleTypeList(articleManager.findAll());
			registerAdminPageClientScriptBlock();
			return SUCCESS;
		}
		if (LForumRequest.getParamValue("ajax").equals("true")) {
			parentTypeList=parentTypeManager.findByFid(LForumRequest.getParamIntValue("fid",-1));
	        List<Dict> dictsList = new ArrayList<Dict>();
	        for (int i = 0; i < parentTypeList.size(); i++) {
	        	dictsList.add(new Dict(parentTypeList.get(i).getId(),parentTypeList.get(i).getName()));
			}
	        JSONArray jsonObject = JSONArray.fromObject(dictsList);
	        dicter = jsonObject.toString();
	        response.setContentType("text/plain;charset=UTF-8");
			response.getWriter().write(dicter);
	        return null;
		}
		if (article!=null) {
			article.setUser(users);
			articleManager.createFartherType(article);
			setArticleTypeList(articleManager.findAll());
			registerAdminPageClientScriptBlock();
			return SUCCESS;
		}
		return null;
	}

	public void registerStartupScript(String key, String scriptstr) {
		key = key.toLowerCase();
		if ((key.equals("pagetemplate")) || (key.equals("page"))) {
			String script = "";
			script += "<input type='hidden' name='submitMethod' value=''/>";
			script += "<script type=\"text/javascript\">function doPost(method){";
			script += "if(method!=''){document.getElementById('submitMethod').value='method:'+method;}";
			script += "var theForm = document.forms['Form1'];";
			script += "if (!theForm) {theForm = document.Form1;}";
			script += "if (!theForm.onsubmit || (theForm.onsubmit() != false)) {theForm.submit();}}</script>";
			htmlBuilder.append(script);
			
			if (key.equals("page")) {
				script = "<script> \r\n" + "var bar=0;\r\n"
						+ "document.getElementById('success').style.display = \"block\";  \r\n"
						+ "document.getElementById('Layer5').innerHTML ='<BR>操作成功执行<BR>';  \r\n" + "count() ; \r\n"
						+ "function count(){ \r\n" + "bar=bar+4; \r\n" + "if (bar<99) \r\n"
						+ "{setTimeout(\"count()\",100);} \r\n" + "else { \r\n"
						+ "document.getElementById('success').style.display = \"none\";HideOverSels('success'); \r\n"
						+ scriptstr + "} \r\n" + "} \r\n" + "</script> \r\n"
						+ "<script> window.onload = function(){HideOverSels('success')};</script>\r\n";
			}

			if (key.equals("pagetemplate")) {
				script = "<script> \r\n" + "var bar=0;\r\n success.style.display = \"block\";  \r\n"
						+ "document.getElementById('Layer5').innerHTML = '<BR>" + scriptstr + "<BR>';  \r\n"
						+ "count() ; \r\n" + "function count(){ \r\n" + "bar=bar+4; \r\n" + "if (bar<99) \r\n"
						+ "{setTimeout(\"count()\",100);} \r\n" + "else { \r\n"
						+ "document.getElementById('success').style.display = \"none\";HideOverSels('success'); \r\n"
						+ "}} \r\n" + "</script> \r\n"
						+ "<script> window.onload = function(){HideOverSels('success')};</script>\r\n";
			}
			htmlBuilder.append(script);

		} else {
			htmlBuilder.append(scriptstr);
		}
	}

	/**
	 * 注册提示信息JS脚本
	 */
	public void registerAdminPageClientScriptBlock() {
		String script = "<div id=\"success\" style=\"position:absolute;z-index:300;height:120px;width:284px;left:50%;top:50%;margin-left:-150px;margin-top:-80px;\">\r\n"
				+ "	<div id=\"Layer2\" style=\"position:absolute;z-index:300;width:270px;height:90px;background-color: #FFFFFF;border:solid #000000 1px;font-size:14px;\">\r\n"
				+ "		<div id=\"Layer4\" style=\"height:26px;background:#f1f1f1;line-height:26px;padding:0px 3px 0px 3px;font-weight:bolder;\">操作提示</div>\r\n"
				+ "		<div id=\"Layer5\" style=\"height:64px;line-height:150%;padding:0px 3px 0px 3px;\" align=\"center\"><BR /><table><tr><td valign=top><img border=\"0\" src=\"../images/ajax_loading.gif\"  /></td><td valign=middle style=\"font-size: 14px;\" >正在执行当前操作, 请稍等...<BR /></td></tr></table><BR /></div>\r\n"
				+ "	</div>\r\n"
				+ "	<div id=\"Layer3\" style=\"position:absolute;width:270px;height:90px;z-index:299;left:4px;top:5px;background-color: #E8E8E8;\"></div>\r\n"
				+ "</div>\r\n"
				+ "<script> \r\n"
				+ "document.getElementById('success').style.display = \"none\"; \r\n"
				+ "</script> \r\n" + "<script type=\"text/javascript\" src=\"../js/divcover.js\"></script>\r\n";
		htmlBuilder.append(script);
	}

	public ArticleManager getArticleManager() {
		return articleManager;
	}

	public void setArticleManager(ArticleManager articleManager) {
		this.articleManager = articleManager;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public List<Article> getArticleTypeList() {
		return articleTypeList;
	}

	public void setArticleTypeList(List<Article> articleTypeList) {
		this.articleTypeList = articleTypeList;
	}

	public StringBuilder getHtmlBuilder() {
		return htmlBuilder;
	}

	public void setHtmlBuilder(StringBuilder htmlBuilder) {
		this.htmlBuilder = htmlBuilder;
	}

	public ParentTypeManager getParentTypeManager() {
		return parentTypeManager;
	}

	public void setParentTypeManager(ParentTypeManager parentTypeManager) {
		this.parentTypeManager = parentTypeManager;
	}

	public List<ParentType> getParentTypeList() {
		return parentTypeList;
	}

	public void setParentTypeList(List<ParentType> parentTypeList) {
		this.parentTypeList = parentTypeList;
	}

	public String getEdit() {
		return edit;
	}

	public void setEdit(String edit) {
		this.edit = edit;
	}

	public String getDicter() {
		return dicter;
	}

	public void setDicter(String dicter) {
		this.dicter = dicter;
	}

	public FartherTypeManager getFartherTypeManager() {
		return fartherTypeManager;
	}

	public void setFartherTypeManager(FartherTypeManager fartherTypeManager) {
		this.fartherTypeManager = fartherTypeManager;
	}

	public List<FartherType> getFartherTypeList() {
		return fartherTypeList;
	}

	public void setFartherTypeList(List<FartherType> fartherTypeList) {
		this.fartherTypeList = fartherTypeList;
	}
}
